<?php

/**
 * This is the model class for table "empresa".
 *
 * The followings are the available columns in table 'empresa':
 * @property integer $id
 * @property integer $activo
 * @property string $razon_social
 * @property string $nit
 * @property string $telefono
 * @property string $representante_legal
 * @property string $correo_representante_legal
 * @property string $celular
 * @property integer $nivel_id
 * @property integer $empresa_padre_id
 *
 * The followings are the available model relations:
 * @property Empresa $empresaPadre
 * @property Empresa[] $empresas
 * @property Nivel $nivel
 * @property Rol[] $rols
 * @property UsuarioEmpresa[] $usuarioEmpresas
 */
class Empresa extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'empresa';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('razon_social, nit, telefono, representante_legal, correo_representante_legal, celular', 'required'),
			array('razon_social, nit, telefono, representante_legal, correo_representante_legal, celular', 'length', 'max'=>255),
			array('correo_representante_legal', 'email'),
			array('razon_social', 'validarRazonSocialNoRegistrado'),
			array('nit', 'validarNitNoRegistrado'),
				
				// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('razon_social, nit, telefono, representante_legal, correo_representante_legal, celular', 'safe', 'on'=>'search'),
		);
	}
	public function validarRazonSocialNoRegistrado($attribute,$params){
		$razon_social=$this->razon_social;
		$command=Yii::app()->db->createCommand("SELECT count(*) FROM empresa e WHERE e.razon_social = :razon_social and e.activo = 1");
		$command->bindParam(":razon_social",$razon_social,PDO::PARAM_STR);
		$count= $command->queryScalar();
	
		if($count > 0){
			$message = Yii::t('yii', '{attribute} "{value}" debe elegir otra razón social.', array('{attribute}'=>$this->getAttributeLabel($attribute),'{value}'=>$this->razon_social));
			$this->addError($attribute, $message);
	
		}
	}
	
	public function validarNitNoRegistrado($attribute,$params){
		$razon_social=$this->nit;
		$command=Yii::app()->db->createCommand("SELECT count(*) FROM empresa e WHERE e.nit = :nit and e.activo = 1");
		$command->bindParam(":nit",$razon_social,PDO::PARAM_STR);
		$count= $command->queryScalar();
	
		if($count > 0){
			$message = Yii::t('yii', '{attribute} "{value}" debe elegir otro nit.', array('{attribute}'=>$this->getAttributeLabel($attribute),'{value}'=>$this->nit));
			$this->addError($attribute, $message);
	
		}
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'empresaPadre' => array(self::BELONGS_TO, 'Empresa', 'empresa_padre_id'),
			'empresas' => array(self::HAS_MANY, 'Empresa', 'empresa_padre_id'),
			'nivel' => array(self::BELONGS_TO, 'Nivel', 'nivel_id'),
			'rols' => array(self::HAS_MANY, 'Rol', 'empresa_id'),
			'usuarioEmpresas' => array(self::HAS_MANY, 'UsuarioEmpresa', 'empresa_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'activo' => 'Activo',
			'razon_social' => 'Razon Social',
			'nit' => 'Nit',
			'telefono' => 'Telefono',
			'representante_legal' => 'Representante Legal',
			'correo_representante_legal' => 'Correo Representante Legal',
			'celular' => 'Celular',
			'nivel_id' => 'Nivel',
			'empresa_padre_id' => 'Empresa Padre',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('activo',$this->activo);
		$criteria->compare('razon_social',$this->razon_social,true);
		$criteria->compare('nit',$this->nit,true);
		$criteria->compare('telefono',$this->telefono,true);
		$criteria->compare('representante_legal',$this->representante_legal,true);
		$criteria->compare('correo_representante_legal',$this->correo_representante_legal,true);
		$criteria->compare('celular',$this->celular,true);
		$criteria->compare('nivel_id',$this->nivel_id);
		$criteria->compare('empresa_padre_id',$this->empresa_padre_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Empresa the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	/**
	 * Attach behaviors.
	 * @return array the behavior configurations (behavior name=>behavior configuration)
	 */
	public function behaviors() {
		return array(
				'EmpresaBehavior'=>array(
						'class'=>'application.behaviors.EmpresaBehavior'
				),
		);
	}
}
